#!/bin/bash

NAME="git-auto-deploy"
GAD_UID="git-auto-deploy"
GAD_GID="git-auto-deploy"

HOME_DIR="/etc/$NAME"
DATA_DIR="/var/lib/$NAME"
PID_FILE="/var/run/$NAME.pid"

# Add user and group
adduser --quiet --system --home $HOME_DIR --no-create-home --ingroup nogroup --disabled-password $GAD_UID
addgroup --system $GAD_GID
adduser $GAD_UID $GAD_GID

# Create home dir
mkdir -p $HOME_DIR
mkdir -p $HOME_DIR/.ssh
touch $HOME_DIR/.ssh/known_hosts
chown -R $GAD_UID:$GAD_GID $HOME_DIR
chmod -R 750 $HOME_DIR

# Create log file
touch /var/log/$NAME.log
chown $GAD_UID:$GAD_GID /var/log/$NAME.log
chmod 750 /var/log/$NAME.log

# Create data directory
mkdir -p $DATA_DIR
chown -R $GAD_UID:$GAD_GID $DATA_DIR
chmod 750 $DATA_DIR

# Create pid file
touch $PID_FILE
chown $GAD_UID:$GAD_GID $PID_FILE
chmod 750 $PID_FILE

update-rc.d $NAME defaults
